/*
* @Author: Administrator
* @Date:   2018-01-17 08:50:38
* @Last Modified by:   Administrator
* @Last Modified time: 2018-01-20 09:02:45
*/
const router = require('express').Router()
const axios = require('axios')
var qs = require('qs');

const baseUrl = 'https://cnodejs.org/api/v1'

router.post('/login', function (req, res) {
  axios.post(`${baseUrl}/accesstoken`, {
    accesstoken: req.body.accessToken,
  }).then(resp => {
    if (resp.status === 200 && resp.data.success) {
      req.session.user = {
        accessToken: req.body.accessToken,
        loginName: resp.data.loginname,
        id: resp.data.id,
        avatarUrl: resp.data.avatar_url,
      }
      res.json({
        success: true,
        data: {
          loginName: resp.data.loginname,
          id: resp.data.id,
          avatarUrl: resp.data.avatar_url,
        }
      })
    }
  }).catch(err => {
    if (err.response) {
      res.json({
        success: false,
        msg: err.message,
      })
    }
  })
})

module.exports = router
